package com.learn.core.controller.learn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.*;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.*;
import com.learn.core.dto.LStudyNotesDTO;
import com.learn.core.service.ILChapterService;
import com.learn.core.service.ILCourseService;
import com.learn.core.service.ILStudyNotesService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName StudyNotesController
 * @date 2021.04.08 10:19
 * @describe 我的笔记
 */
@Api("我的笔记")
@RestController
@RequestMapping("/learn/snotes")
public class StudyNotesController extends WebController<LStudyNotes> {
    @Autowired
    private ILStudyNotesService ilStudyNotesService;
    @Autowired
    private ILChapterService ilChapterService;
    @Autowired
    private ILCourseService ilCourseService;

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //我的笔记列表
    @ApiOperation(value = "我的笔记列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<LStudyNotes> list(@RequestBody LStudyNotesDTO lStudyNotesDTO){
        try {
            if(lStudyNotesDTO==null||lStudyNotesDTO.getPage()==null||lStudyNotesDTO.getRows()==null){
                return new PageResult<LStudyNotes>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lStudyNotesDTO.getPage(),lStudyNotesDTO.getRows());
            QueryWrapper<LStudyNotes> queryWrapper = new QueryWrapper<LStudyNotes>();
            if(lStudyNotesDTO.getCourseId() != null)
                queryWrapper.eq("course_id",lStudyNotesDTO.getCourseId());
            if(lStudyNotesDTO.getChapterId()!=null)
                queryWrapper.eq("chapter_id",lStudyNotesDTO.getChapterId());
            if(lStudyNotesDTO.getUserId()!=null)
                queryWrapper.eq("create_user",lStudyNotesDTO.getUserId());
            Page<LStudyNotes> lStudyNotesPage = (Page<LStudyNotes>) ilStudyNotesService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lStudyNotesPage)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            for (LStudyNotes lStudyNotes : lStudyNotesPage) {
                LChapter lChapter = ilChapterService.getById(lStudyNotes.getChapterId());
                if(null != lChapter){
                    lStudyNotes.setChapterName(lChapter.getChapterName());
                }
                LCourse lCourse = ilCourseService.getById(lStudyNotes.getCourseId());
                if(null != lCourse){
                    lStudyNotes.setCourseName(lCourse.getCourseName());
                }
            }
            // 返回
            return new PageResult(lStudyNotesPage.getTotal(), lStudyNotesPage, ResultEnum.SUCCESS);
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加学习笔记", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LStudyNotes lStudyNotes, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lStudyNotes!=null) {
                if (StringUtils.isEmpty(lStudyNotes.getChapterId()))
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_CHAPTER_ID);
                if (StringUtils.isEmpty(lStudyNotes.getCourseId()))
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_COURSE_ID);
                if (StringUtils.isEmpty(lStudyNotes.getStudyNotesTime()))
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_STUDY_NOTES_TIME);
                lStudyNotes.setCreateTime(new Date());
                lStudyNotes.setCreateUser(userId);
                if (ilStudyNotesService.save(lStudyNotes)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除学习笔记", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LStudyNotes lStudyNotes){
        try {
            if(lStudyNotes!=null){
                if(ilStudyNotesService.removeById(lStudyNotes.getStudyNotesId())){
                    return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e) {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新学习笔记", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LStudyNotes lStudyNotes, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lStudyNotes!=null){
            lStudyNotes.setUpdateTime(new Date());
            lStudyNotes.setUpdateUser(userId);
            if(ilStudyNotesService.updateById(lStudyNotes)){
                return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}
